﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Text;
using System.Collections.Generic;

public partial class PushMoneyManage_PushMoney : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["FCode"] = "300500101";
        if (!IsPostBack)
        {
            Tbx_Man.Attributes.Add("onclick", "MultiSearchVIP('../');");
        }
    }
    private void BInd()
    {
        StringBuilder Sb_Where = new StringBuilder();
        #region 组合where条件
        if (DDL_Month.SelectedIndex > 0)
        {
            Sb_Where.AppendFormat("TiChenApply.App_Month='{0}'", DDL_Month.SelectedValue);
        }
        if (Tbx_Man.Text.Trim().Length > 0)
        {
            if (Tbx_Man.Text.IndexOf(',') > 0)
            {
                Sb_Where.AppendFormat(" and App_Code in (SELECT * FROM dbo.[Split]({0},','))", Tbx_Man.Text.Trim());
            }
            else
            {
                Sb_Where.AppendFormat(" and App_Code='{0}'", Tbx_Man.Text.Trim());
            }
        }
        if (Tbx_TimeFrom.Text.Trim().Length > 0 && Tbx_TimeTo.Text.Trim().Length < 1)
        {

            Sb_Where.AppendFormat(" and App_Date>='{0}'", Tbx_TimeFrom.Text.Trim());

        }
        if (Tbx_TimeFrom.Text.Trim().Length < 1 && Tbx_TimeTo.Text.Trim().Length > 0)
        {

            Sb_Where.AppendFormat("and App_Date<='{0}'", Tbx_TimeTo.Text.Trim());

        }
        if (Tbx_TimeFrom.Text.Trim().Length > 0 && Tbx_TimeTo.Text.Trim().Length > 0)
        {

            Sb_Where.AppendFormat("and App_Date<='{0}' and App_Date<='{1}'", Tbx_TimeFrom.Text.Trim(), Tbx_TimeTo.Text.Trim());

        }
        if (DDL_Valid.SelectedIndex > 0)
        {

            Sb_Where.AppendFormat("and App_Valid={0}", DDL_Valid.SelectedValue);

        }
        if (DDL_ZF.SelectedIndex > 0)
        {

            Sb_Where.AppendFormat("and App_IsDel={0}", DDL_ZF.SelectedValue);

        }
        #endregion
        int pagecount, counts;
        List<Model.TiChenApply> l = new BLL.TiChenApply().GetList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, "APP_ID", false, Sb_Where.ToString(), out pagecount, out counts);
        TiChenApplyListControl1.Bind(l);
        AspNetPager1.RecordCount = counts;
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BInd();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        BInd();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        int count = 0; int checkCount = 0;
        List<int> items = TiChenApplyListControl1.Selected;
        if (items.Count > 0)
        {
            foreach (int item in items)
            {
                Model.TiChenApply ticheng = new BLL.TiChenApply().GetModel(item);

                if (ticheng != null)
                {
                    if (ticheng.App_Valid == 0)
                    {

                        Model.V_VIPSET vipset = new BLL.V_VIPSET().GetModelByGKDM(ticheng.App_Code);

                        Model.V_VIP_ACCOUNT account = new BLL.V_VIP_ACCOUNT().GetModel(vipset.DM);

                        decimal balance = ticheng.App_FactAmt;
                        if (account != null)
                        {
                            balance = (account.DQJE_1 + ticheng.App_FactAmt);
                        }

                        StringBuilder strSql = new StringBuilder("update TiChenApply set App_Valid=1,App_ValidMan='" + Share.UserValue(0) + "',App_ValidDate='" + DateTime.Now + "' where App_ID=" + item.ToString() + " and App_IsDel=0 and App_Valid=0");
                        strSql.Append(" UPDATE BankRollDayToDayAccount SET [BDDA_SendFlag]=1 WHERE [BDDA_ID] IN (SELECT [BYZD5] FROM [TiChenMX] WHERE [TC_ID]=" + item.ToString() + ")");
                        strSql.Append(" if not exists(select top 1 BA_ID from BALANCEADJUST where BA_FIELD1='" + item.ToString() + "') insert into BALANCEADJUST(BA_ID,BA_RID,BA_TYPE,BA_CREATER,BA_CREATEDATE,BA_FIELD1) values('" + BLL.Common.GetDM("300400102") + "',null,'010','" + Share.UserValue(0) + "','" + DateTime.Now + "'," + item.ToString() + ")");
                        strSql.Append(" if not exists(select top 1 BAD_ID from BADETAIL where BAD_ID in (select BA_ID from BALANCEADJUST where BA_FIELD1='" + item.ToString() + "')) insert into BADETAIL(BAD_ID,BAD_DETAILCODE,BAD_CUSTOMERCODE,BAD_AMT,BAD_BALANCE) select'" + BLL.Common.GetDM("300400102") + "',1,'" + vipset.GKDM + "',App_FactAmt as BAD_AMT," + balance + " FROM TiChenApply WHERE App_ID=" + item.ToString());//插入一条调整单明细
                        if (new BLL.TiChenApply().AuditApply(strSql.ToString()) > 0)
                        {
                            count++;
                            BLL.Common.ADDLSH("300400102");
                            BInd();

                        }
                    }
                }
            }
            if (count == items.Count && count != 0)
            {
                BLL.Common.Alert(this.Page, "审核成功，同时生成了相关的结余款调整单需要审核");
            }
            else if (count == 0)
            {
                BLL.Common.Alert(this.Page, "单据已被审核，不允许再次操作！");
            }
            else
            {
                BLL.Common.Alert(this.Page, "部分审核成功，同时生成了相关的结余款调整单需要审核");
            }
        }
        else
        {
            BLL.Common.Alert(this.Page, "请选择一条需要审核的记录");
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        List<int> items = TiChenApplyListControl1.Selected;
        if (items.Count > 0)
        {
            foreach (int item in items)
            {
                Model.TiChenApply ticheng = new BLL.TiChenApply().GetModel(item);

                if (ticheng != null)
                {
                    if (ticheng.App_Valid == 0)
                    {
                        string sql = "update TiChenApply set App_DelMan='" + Share.UserValue(0) + "' ,App_IsDel=1 ,App_DelDate='" + DateTime.Now + "' where App_ID=" + item.ToString() + " and App_Valid=0";
                        sql = sql + " UPDATE BankRollDayToDayAccount SET [BDDA_SendFlag]=0 WHERE [BDDA_ID] IN (SELECT [BYZD5] FROM [TiChenMX] WHERE [TC_ID]=" + item.ToString() + ")";
                        new BLL.TiChenApply().DeleteApply(sql);
                    }
                }
            }
            BInd();
        }
    }
}
